﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bloodbank.Api;
using Bloodbank.Dto;

namespace Bloodbank
{
    public partial class frmUserUpdate : Form
    {
        private int UserId;
        public frmUserUpdate(int id)
        {
            UserId = id;
            InitializeComponent();
        }

        private void LoadCbbGroupBlood()
        {
            BloodGroupApi bloodGroupApi = new BloodGroupApi();
            cbbBloodgroup.DisplayMember = "BloodGroupName";
            cbbBloodgroup.ValueMember = "Id";
            cbbBloodgroup.DataSource = bloodGroupApi.GetGroupBloodList();
        }
        private void frmUserUpdate_Load(object sender, EventArgs e)
        {
            LoadCbbGroupBlood();

            UserDto user = new UserDto();
            UserApi userApi = new UserApi();
            user = userApi.GetUserById(UserId);

            txtUsername.Text = user.Username;
            txtPassword.Text = user.Password;
            txtConfirmPassword.Text = user.ConfirmPassword;
            txtFullname.Text = user.FullName;
            txtEmail.Text = user.Email;
            txtAddress.Text = user.Address;
            txtCity.Text = user.City;
            txtState.Text = user.State;
            if ((bool)user.Gender){
                radMale.Checked = true;
            }
            else{
                radFreemale.Checked = true;
            }
            txtPhone.Text = user.Phone;
            dtpBirthday.Value = (DateTime)user.Birthday;
            cbbBloodgroup.SelectedValue = user.BloodGroupId;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Username is required", "Message");
            }
            else if (txtPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required", "Message");
            }
            else if (txtConfirmPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required", "Message");
            }
            else if (txtFullname.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Fullname is required", "Message");
            }
            else if (txtConfirmPassword.Text.Trim() != txtPassword.Text.Trim())
            {
                MessageBox.Show("Password is incorrect", "Message");
            }
            else
            {
                UserDto user = new UserDto()
                {
                    Id = UserId,
                    Username = txtUsername.Text.Trim(),
                    Password = txtPassword.Text.Trim(),
                    ConfirmPassword = txtConfirmPassword.Text.Trim(),
                    Email = txtEmail.Text.Trim(),
                    FullName = txtFullname.Text.Trim(),
                    Address = txtAddress.Text.Trim(),
                    City = txtCity.Text.Trim(),
                    State = txtState.Text.Trim(),
                    Gender = radMale.Checked ? true : false,
                    Phone = txtPhone.Text.Trim(),
                    Birthday = DateTime.Parse(dtpBirthday.Value.ToString()),
                    BloodGroupId = int.Parse(cbbBloodgroup.SelectedValue.ToString())
                };
                UserApi userApi = new UserApi();
                userApi.Updated(user);
                MessageBox.Show("Account is updated successfully", "Message");
            }
        }
    }
}
